﻿namespace FDSimpleModelGenerator.Common
{
    /// <summary>
    /// 数据库数据类型映射
    /// </summary>
    public class DbTypeMapper
    {
        /// <summary>
        /// 映射数据库类型成C#类型
        /// </summary>
        /// <param name="dbType">数据库类型</param>
        /// <param name="isUnsigned">无符号</param>
        /// <returns></returns>
        public static string Map(string dbType, bool isUnsigned)
        {
            dbType = dbType.ToLower();
            switch (dbType)
            {
                case "int":
                case "integer":
                case "serial":
                    return isUnsigned ? "uint" : "int";
                case "decimal":
                case "real":
                case "money":
                case "double precision":
                case "numeric":
                case "smallmoney":
                    return "decimal";
                case "bit":
                    return "bool";
                case "bigint":
                case "bigserial":
                    return isUnsigned ? "ulong" : "long";
                case "float":
                    return "float";
                case "smallint":
                case "tinyint":
                    return isUnsigned ? "ushort" : "byte";
                case "smallserial":
                    return "ushort";
                case "datetime":
                case "datetime2":
                case "smalldatetime":
                case "date":
                    return "DateTime";
                case "time":
                    return "TimeSpan";
                case "nvarchar":
                case "varchar":
                case "char":
                default:
                    return "string";
            }
        }
    }
}
